package com.js.nickcar.module.addcar.uploadphoto;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.js.nickcar.R;
import com.js.nickcar.base.BaseHolder;
import com.js.nickcar.entity.local.UploadCarPhotoBean;
import com.js.nickcar.entity.local.UploadPhotoBean;

import java.util.ArrayList;


/**
 * Created by LZY on 2017/10/7.
 */
public class UploadCarPhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements CarPhotoAdapter.OnCarPhotoListener, UploadCarPhotoBean.Type {

    public final Context mContext;
    private OnUploadCarPhotoListener onUploadCarPhotoListener;

    public final LayoutInflater mInflater;
    public static final int VIEW_TYPE_NOR = 0;

    public static final int VIEW_TYPE_NEXT = 1;

    ArrayList<UploadCarPhotoBean> mData = new ArrayList<>();

    {
        mData.add(new UploadCarPhotoBean(TYPE_APPEARANCE, "车辆外观"));
        mData.add(new UploadCarPhotoBean(TYPE_INTERIOR, "车辆内饰"));
        mData.add(new UploadCarPhotoBean(TYPE_ENGINE, "结构及发动机"));
        mData.add(new UploadCarPhotoBean(TYPE_OTHER, "其他细节"));
    }

    public UploadCarPhotoAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_NOR:
                UploadCarPhotoHolder uploadCarPhotoHolder = new UploadCarPhotoHolder(mInflater.inflate(R.layout.item_upload_car_photo, parent, false));
                uploadCarPhotoHolder.rvPhoto.setLayoutManager(new GridLayoutManager(mContext, 3));
                CarPhotoAdapter carPhotoAdapter = new CarPhotoAdapter(mContext);
                carPhotoAdapter.setOnCarPhotoListener(this);
                uploadCarPhotoHolder.rvPhoto.setAdapter(carPhotoAdapter);
                return uploadCarPhotoHolder;
            default:
                return new NextHolder(mInflater.inflate(R.layout.item_upload_car_next, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_NOR:
                onBindUploadCarPhotoHolder((UploadCarPhotoHolder) holder, position);
                break;
            default:
                onBindUNextHolder((NextHolder) holder);
                break;
        }
    }

    public void onBindUploadCarPhotoHolder(UploadCarPhotoHolder holder, int position) {
        UploadCarPhotoBean item = getItem(position);
        holder.tvTitle.setText(item.getTitle());
        CarPhotoAdapter adapter = (CarPhotoAdapter) holder.rvPhoto.getAdapter();
        adapter.setData(item.getUploadPhotos(), item.getType());
    }

    public UploadCarPhotoBean getItem(int position) {
        return mData.get(position);
    }


    public ArrayList<UploadCarPhotoBean> getItems() {
        return mData;
    }

    private void onBindUNextHolder(NextHolder holder) {
        holder.btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onUploadCarPhotoListener != null) {
                    onUploadCarPhotoListener.onNext();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mData.size()) {
            return VIEW_TYPE_NEXT;
        } else {
            return VIEW_TYPE_NOR;
        }
    }

    @Override
    public void onAddPhoto(UploadPhotoBean uploadPhotoBean) {
        if (onUploadCarPhotoListener != null) {
            onUploadCarPhotoListener.onAddPhoto(uploadPhotoBean);
        }
    }

    @Override
    public void remove(String type, UploadPhotoBean item, int position) {
        for (UploadCarPhotoBean uploadCarPhotoBean : mData) {
            if (uploadCarPhotoBean.getType().equals(type)) {
                if (uploadCarPhotoBean.getUploadPhotos().contains(item)) {
                    uploadCarPhotoBean.getUploadPhotos().remove(item);
                }
            }
        }
    }

    public void setOnUploadCarPhotoListener(OnUploadCarPhotoListener onUploadCarPhotoListener) {
        this.onUploadCarPhotoListener = onUploadCarPhotoListener;
    }

    public interface OnUploadCarPhotoListener {
        void onAddPhoto(UploadPhotoBean uploadPhotoBean);

        void onNext();
    }

    public static class UploadCarPhotoHolder extends BaseHolder {

        public final TextView tvTitle;
        public final RecyclerView rvPhoto;

        public UploadCarPhotoHolder(View itemView) {
            super(itemView);
            tvTitle = findViewById(R.id.tv_title);
            rvPhoto = findViewById(R.id.rv_photo);
        }
    }

    public static class NextHolder extends BaseHolder {
        private final Button btnNext;

        public NextHolder(View view) {
            super(view);
            btnNext = findViewById(R.id.btn_next);
        }
    }
}
